var pageNum;
$(function () {

    initLayerDate("#startDate",true);
    afficheInfo();
    queryAjaxAfficheList(1);
    $('body').on('click','#submitFrom,#querySubmit',function () {
        //点击搜索查询从第一页开始查询
        queryAjaxAfficheList(1);
    });
    //单个删除
    $('body').on('click','.deleteAffiche',function () {
        var afficheIds = $(this).data('id');
        var obj = $(this);
        icModale(2,"确认要删除所选公告么？",3,function () {
            updateAfficheStatusByAfficheIds(afficheIds,obj,2);
        });
    });
    //批量删除
    $("body").on("click","#deleteBatch",function () {
        var obj = $("input[name='afficheId']:checked").parents('tr');
        var afficheIds = obtainIds(obj);//获取勾选ids
        if(!$.isEmptyObject(afficheIds)){
            icModale(2,"确认要删除所选公告么？",3,function () {
                updateAfficheStatusByAfficheIds(afficheIds,obj,2);
            });
        }
    });
    //批量冻结
    $('body').on("click", "#unlockBatch", function () {
        var obj = $("input[name='afficheId']:checked").parents('tr');
        var afficheIds = obtainIds(obj);//获取勾选ids
        if (!$.isEmptyObject(afficheIds)) {
            updateAfficheStatusByAfficheIds(afficheIds, obj, 1);
        }
    });

    //批量恢复
    $("body").on("click", "#historyBatch", function () {
        var obj = $("input[name='afficheId']:checked").parents('tr');
        var afficheIds = obtainIds(obj);//获取勾选ids
        if (!$.isEmptyObject(afficheIds)) {
            updateAfficheStatusByAfficheIds(afficheIds, obj, 0);
        }
    });
    //单个冻结
    $('body').on('click', '.unlockAndHistory', function () {
        var afficheIds = $(this).data('id');
        var status = $(this).data('status');
        var obj = $(this);
        if (status == 0) {
            updateAfficheStatusByAfficheIds(afficheIds, obj, 1);
        }
        if (status == 1) {
            updateAfficheStatusByAfficheIds(afficheIds, obj, 0);
        }
    });
    //拼接id串
    function obtainIds(obj) {
        if(null == obj || obj.size() == 0){
            icModale(0,"请选择要批量操作的数据");
            return null;
        }
        var articleIds = "";
        $(obj).each(function (index,_input) {
            articleIds += $(_input).data('id') + ",";
        });
        articleIds = articleIds.substring(0,articleIds.length-1);
        return articleIds;
    }

    /**
     * 获取角色列表页面
     * @param pageNo 当前页码
     */
    function queryAjaxAfficheList(pageNo) {
        var params = $("#search-form").serializeJSON();
        params = JSON.stringify(params);
        var obj = {
            url: baseloaction + '/admin/op/affiche/queryAjaxAfficheList',//请求URL
            contextId: 'affiche-list-context',//响应内容页面设置内容ID
            params: params,//请求参数
            pageNo: pageNo,//页码
            pageCall: queryAjaxAfficheList, //分页回调加载数据(点击上下页时调用,该方法也在加载页面完成时调用)
            selectAllId: 'select-all-affiche', //全选框ID
            callback: function (pageNo) {//数据加载完成回调
                pageNum = pageNo;
                $("#afficheCount").html(total);
            }
        };
        ajaxGetListData(obj)
    }

    /**
     * 批量封禁，批量恢复
     * @param userIds
     * @param obj
     * @param isavalible
     */
    function updateAfficheStatusByAfficheIds(afficheIds, obj, status) {
        $.ajax({
            url: baseloaction + "/admin/op/affiche/updateAfficheStatusByIds",
            data: {"afficheIds": afficheIds, "status": status},
            type: "post",
            dataType: "json",
            success: function (result) {
                if (result.success) {
                    if (1 == status) {
                        obj.find('.status').html('');
                        obj.find('.status').append('<span class="label label-danger">冻结</span>');
                        obj.find(".unlockAndHistory").html("");
                        obj.find(".unlockAndHistory").data('status', '1');
                        /*obj.find(".unlockAndHistory").append('<i class="fa fa-history"></i>')*/
                    }
                    if (0 == status) {
                        obj.find('.status').html('');
                        obj.find('.status').append('<span class="label label-info">正常</span>');
                        obj.find(".unlockAndHistory").html("");
                        obj.find(".unlockAndHistory").data('status', '0');
                        /*obj.find(".unlockAndHistory").append('<i class="fa fa-unlock-alt"></i>')*/
                    }
                    if (2 == status) {
                        queryAjaxAfficheList(pageNum);
                    }
                } else {
                    icModale(0, result.message);
                }
            },
            error: function (error) {
                icModale(0, "系统繁忙,请稍后重试");
            }
        })
    }

    /**
     * 消息详情
     */
    function afficheInfo() {
        $('body').on('click', '.afficheInfo', function () {
            var afficheId = $(this).data('id');
            var index = parent.layer.open({
                type: 2,
                shadeClose: false,
                shade: [0.3, '#000000'],
                title: '<strong>消息详情</strong>',
                area: ['75%', '75%'],
                content: baseloaction + "/admin/op/affiche/toShowAffiche?afficheId="+afficheId,
                btn: ['关闭'],
                yes: function (index, layero) {
                    parent.layer.close(index);
                }
            });
        })
    }
});
